#!/bin/bash -e

set -e

# Source variables from config.conf file
. config.conf


##############################################################################
############################      FUNCTIONS     ##############################
##############################################################################

function initialSetup {
    NDK_PATH="$DOWNLOAD_DIR/$NDK_DIR_NAME"
    TOOLCHAIN=${NDK_PATH}/toolchains/llvm/prebuilt/linux-x86_64
    SDK_TOOLS_PATH="$DOWNLOAD_DIR/${SDK_DIR_NAME}"/tools
    FFMPEG_SRC_PATH="$DOWNLOAD_DIR/${FFMPEG_DIR_NAME}/FFmpeg-n${FFMPEG_VERSION}"
    FFMPEG_TMP_DIR="/tmp/ffmpeg"
}

function setupPathsAndExports {
    FFMPEG_LIB_PATH="${FFMPEG_BUILD_OUT_PATH}/libs"
    FFMPEG_LOG_PATH="${FFMPEG_BUILD_OUT_PATH}/logs"

    export ANDROID_NDK_HOME=$NDK_PATH
    export ANDROID_HOME=$DOWNLOAD_DIR/${SDK_DIR_NAME}

    export PATH=${SDK_TOOLS_PATH}:$PATH
}

function clearBuildDirectory {
    rm -rf "${FFMPEG_BUILD_OUT_PATH}"
    mkdir -p "${FFMPEG_LIB_PATH}"
    mkdir -p "${FFMPEG_LOG_PATH}"
}

function clearTmpAndInitDirectory {
    rm -rf "${FFMPEG_TMP_DIR}"
    mkdir -p "${FFMPEG_TMP_DIR}"
    cd ${FFMPEG_SRC_PATH}
    cp -r * ${FFMPEG_TMP_DIR}
    cd ${FFMPEG_TMP_DIR}
    mkdir -p "$BUILD_DIR"
    mkdir -p "${FFMPEG_LIB_PATH}/${arch}"
    mkdir -p "${FFMPEG_LOG_PATH}"
}

function finalizeArgs {
    arch=$1
    if [ "$arch" == "armeabi" ]
    then
        FFMPEG_ARCH="arm"
        FFMPEG_CPU="armv7-a"
        FFMPEG_COMPILER_PREFIX="armv7a"
        FFMPEG_API=eabi${FFMPEG_TARGET_NDK_LEVEL}
        FFMPEG_CROSS_PREFIX=${TOOLCHAIN}/bin/arm-linux-androideabi-
        FFMPEG_ADDITIONAL_CONFIGURE_FLAG="--enable-neon"
    elif [ "$arch" == "armeabi-v7a" ]
    then
        FFMPEG_ARCH="arm"
        FFMPEG_CPU="armv7-a"
        FFMPEG_COMPILER_PREFIX="armv7a"
        FFMPEG_API=eabi${FFMPEG_TARGET_NDK_LEVEL}
        FFMPEG_CROSS_PREFIX=${TOOLCHAIN}/bin/arm-linux-androideabi-
        FFMPEG_ADDITIONAL_CONFIGURE_FLAG="--enable-neon"
    elif [ "$arch" == "x86" ]
    then
        FFMPEG_ARCH="x86"
        FFMPEG_CPU="i686"
        FFMPEG_COMPILER_PREFIX="i686"
        FFMPEG_API=${FFMPEG_TARGET_NDK_LEVEL}
        FFMPEG_CROSS_PREFIX=${TOOLCHAIN}/bin/i686-linux-android-
        FFMPEG_ADDITIONAL_CONFIGURE_FLAG="--disable-asm"
    elif [ "$arch" == "x86_64" ]
    then
        FFMPEG_ARCH="x86_64"
        FFMPEG_CPU="x86-64"
        FFMPEG_COMPILER_PREFIX="x86_64"
        FFMPEG_API=${FFMPEG_TARGET_NDK_LEVEL}
        FFMPEG_CROSS_PREFIX=${TOOLCHAIN}/bin/x86_64-linux-android-
        FFMPEG_ADDITIONAL_CONFIGURE_FLAG="--disable-asm"
    elif [ "$arch" == "arm64-v8a" ]
    then
        FFMPEG_ARCH="aarch64"
        FFMPEG_CPU="armv8-a"
        FFMPEG_COMPILER_PREFIX="aarch64"
        FFMPEG_API=${FFMPEG_TARGET_NDK_LEVEL}
        FFMPEG_CROSS_PREFIX=${TOOLCHAIN}/bin/aarch64-linux-android-
        FFMPEG_ADDITIONAL_CONFIGURE_FLAG="--enable-neon"
    else
        echo "Unsupported target ABI: $arch"
        exit 1
    fi
}

function build_android {
    FFMPEG_CC=$TOOLCHAIN/bin/${FFMPEG_COMPILER_PREFIX}-linux-android${FFMPEG_API}-clang
    FFMPEG_CXX=$TOOLCHAIN/bin/${FFMPEG_COMPILER_PREFIX}-linux-android${FFMPEG_API}-clang++
    FFMPEG_SYSROOT=$TOOLCHAIN/sysroot
    FFMPEG_PREFIX=${FFMPEG_LIB_PATH}/${arch}
    FFMPEG_OPTIMIZE_CFLAGS="-march=$FFMPEG_CPU"

    echo "Compiling FFmpeg for $FFMPEG_CPU"

    ./configure \
        --prefix=$FFMPEG_PREFIX \
        --enable-gpl \
        --disable-shared \
        --enable-static \
        --disable-small \
        --disable-programs \
        --disable-doc \
        --disable-avdevice \
        --disable-swresample \
        --disable-postproc \
        --disable-decoder=h264_mediacodec \
        --enable-hwaccels \
        --disable-mediacodec \
        --disable-symver \
        --enable-jni \
        --cross-prefix=$FFMPEG_CROSS_PREFIX \
        --target-os=android \
        --arch=$FFMPEG_ARCH \
        --cc=$FFMPEG_CC \
        --cxx=$FFMPEG_CXX \
        --enable-cross-compile \
        --sysroot=$FFMPEG_SYSROOT \
        --extra-cflags="-Os -fpic" \
        --extra-ldflags="$ADDI_LDFLAGS" \
        $FFMPEG_ADDITIONAL_CONFIGURE_FLAG
    
    echo "Configure complete!"
    make clean >> "${FFMPEG_LOG_PATH}/${arch}.log" 2>&1
    echo "Start building..."
    make -j4 >> "${FFMPEG_LOG_PATH}/${arch}.log" 2>&1
    echo "Start installing..."
    make install >> "${FFMPEG_LOG_PATH}/${arch}.log" 2>&1
    echo "The Compilation of FFmpeg for $FFMPEG_CPU is completed"
}


##############################################################################
############################        INIT          ############################
##############################################################################

# Initial variables setup
initialSetup
# Set final paths and exports
setupPathsAndExports
# Clear and recreate the build output directory
clearBuildDirectory


##############################################################################
############################        MAIN          ############################
##############################################################################

for arch in "${TARGET_ARCHS[@]}"
do
    echo "Building FFmpeg for target arch $arch ..."
    # Clear the tmp source directory
    clearTmpAndInitDirectory

    # Add final architecture dependent info
    finalizeArgs $arch

    build_android
done

echo "Finished building FFmpeg! Check output folder: ${FFMPEG_BUILD_OUT_PATH}"

set +e
